﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ly_thuyet_toi_uu
{
    class clsFindMax
    {
        double f(double x1, double x2)
        {
            return 2 * x1 * x1 * x1 + 5 * x2 * x2;
        }


        public int Run()
        {
            double[] x1, x2;
            double dtx1, dtx2, fx;
            double x1_b, x2_b;
            double alpha;
            double x1k, x2k;
            double x1b, x2b;
            double fmax;
            int i, j, k, k2, n = 77;

            x1 = new double[100];
            x2 = new double[100];

            //printf("Nhap so buoc n=");  scanf("%d",&n);
            // Tham do buoc 1
            // 1. Chon gia gi ban dau
            x1[0] = 0.5;
            x2[0] = 1;
            dtx1 = 0.1;
            dtx2 = 0.4;
            // Tinh fx(0)
            fx = f(x1[0], x2[0]);

            // 2. Bien doi x1, x2 giu nguyen
            x1[1] = x1[0] + dtx1;
            alpha = f(x1[1], x2[0]);

            // 3. Neu alpha<f(x(0)). x1[1]=x1[0]-dtx1. Tinh lai alpha
            // Neu khong cai thien duoc ca hai phia thi co dinh x1
            if (alpha < fx)
            {
                x1[1] = x1[0] - dtx1;
                alpha = f(x1[1], x2[0]);
                fx = alpha;
            }
            else
            {
                x1[1] = x1[0];
            }
            //4. Tiep tuc bien doi x2
            i = 0;
            while (dtx1 > 0.001)
            {
                i = i + 1;
                j = i - 1;
                x2[1] = x2[j] + dtx2;
                alpha = f(x1[i], x2[i]);
                if (alpha < fx)
                {
                    x2[i] = x2[j] - dtx2;
                    alpha = f(x1[i], x2[i]);
                }
                else x2[i] = x2[j];

                // x1k=x1[i];
                //x2k=x2[i]; 
                // ======================
                //B. Tim theo mau 
                //Lay x[k+1]=t*x[k]-xb; 
                // t=2 vi co hai bien, xb=x0

                x1b = x1[j];
                x2b = x2[j];
                fx = alpha;
                k = i + 1;
                x1[k] = 2 * x1[i] - x1b;
                x2[k] = 2 * x2[i] - x2b;
                double fx_m = f(x1[k], x2[k]);
                // Tham do buoc 2
                // x[k+2]
                k2 = i + 2;
                x1[k2] = x1[k] + dtx1; fmax = f(x1[k2], x2[k]);
                x1[k2] = x1[k] - dtx1; alpha = f(x1[k2], x2[k]);
                if (fmax < alpha) fmax = alpha;
                x2[k2] = x2[k] + dtx2; alpha = f(x1[k2], x2[k]);
                if (fmax < alpha) fmax = alpha;
                x2[k2] = x2[k] - dtx2; alpha = f(x1[k2], x2[k]);
                if (fmax < alpha) fmax = alpha;
                // So sanh 
                if (fmax > fx)
                {
                    x1b = x1[i];
                    x2b = x2[i];
                }
                else
                {
                    dtx1 = dtx1 - 0.01;
                    dtx2 = dtx2 - 0.01;
                }

            }

            // WriteLine("\n %d",i);
            Console.WriteLine("\n Gia tri lon nhat ham fmax={0} %5.3f", fx);
            //system("pause");
            Console.ReadLine();
            return 0;

        }
    }
}
